{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "transform_specs_json = \"\"\"\n",
    "        {\"aggregation_params_map\":{\n",
    "               \"aggregation_pipeline\":{\"source\":\"streaming\",\n",
    "                                       \"usage\":\"fetch_quantity\",\n",
    "                                       \"setters\":[\"rollup_quantity\",\n",
    "                                                  \"set_aggregated_metric_name\",\n",
    "                                                  \"set_aggregated_period\"],\n",
    "                                       \"insert\":[\"prepare_data\",\n",
    "                                                 \"insert_data\"]},\n",
    "               \"aggregated_metric_name\": \"vcpus_agg\",\n",
    "               \"aggregation_period\": \"hourly\",\n",
    "               \"aggregation_group_by_list\": [\"host\", \"metric_id\", \"tenant_id\"],\n",
    "               \"usage_fetch_operation\": \"latest\",\n",
    "               \"setter_rollup_group_by_list\": [\"tenant_id\"],\n",
    "               \"setter_rollup_operation\": \"sum\",\n",
    "\n",
    "               \"dimension_list\":[\"aggregation_period\",\n",
    "                                 \"host\",\n",
    "                                 \"project_id\"]\n",
    "         },\n",
    "         \"metric_group\":\"vcpus_project\",\n",
    "         \"metric_id\":\"vcpus_project\"}\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dict_object=json.loads(transform_specs_json)# json string -> dict object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(dict_object)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "s=json.dumps(dict_object)# dict object->string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with open('dump.json','w') as f:\n",
    "    json.dump(s,f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 驱动器 D 中的卷没有标签。\n",
      " 卷的序列号是 2E66-0EF2\n",
      "\n",
      " D:\\github\\A_DJH\\DJH-Python\\libs\\json 的目录\n",
      "\n",
      "2018/03/13  12:28    <DIR>          .\n",
      "2018/03/13  12:28    <DIR>          ..\n",
      "2018/03/13  11:46    <DIR>          .ipynb_checkpoints\n",
      "2018/03/13  12:26                 0 data.json\n",
      "2018/03/13  12:28               684 dump.json\n",
      "2018/03/13  12:28             8,235 Untitled.ipynb\n",
      "               3 个文件          8,919 字节\n",
      "               3 个目录 90,138,595,328 可用字节\n"
     ]
    }
   ],
   "source": [
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with open('data.json','r') as f:\n",
    "    dict_object=json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'aggregation_params_map': {'aggregated_metric_name': 'vcpus_agg',\n",
       "  'aggregation_group_by_list': ['host', 'metric_id', 'tenant_id'],\n",
       "  'aggregation_period': 'hourly',\n",
       "  'aggregation_pipeline': {'insert': ['prepare_data', 'insert_data'],\n",
       "   'setters': ['rollup_quantity',\n",
       "    'set_aggregated_metric_name',\n",
       "    'set_aggregated_period'],\n",
       "   'source': 'streaming',\n",
       "   'usage': 'fetch_quantity'},\n",
       "  'dimension_list': ['aggregation_period', 'host', 'project_id'],\n",
       "  'setter_rollup_group_by_list': ['tenant_id'],\n",
       "  'setter_rollup_operation': 'sum',\n",
       "  'usage_fetch_operation': 'latest'},\n",
       " 'metric_group': 'vcpus_project',\n",
       " 'metric_id': 'vcpus_project'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
